package com.yanqu.road.logic.helper;

import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.rank.UnionValueRank;
import com.yanqu.road.entity.rank.cross.CrossActivityTeamRank;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.entity.rank.cross.CrossUnionWeekRank;
import com.yanqu.road.entity.rank.cross.CrossUserRank;

import java.util.*;

public class RankListHelper {
    /**
     * 校验数组，去掉重复数据
     */
    public static void checkUserRankListValid(LinkedList<CrossUserRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        List<Long> hasUserIdList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            CrossUserRank userRank = rankList.get(i);
            if(!hasUserIdList.contains(userRank.getUserId())){
                hasUserIdList.add(userRank.getUserId());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    /**
     * 校验数组，去掉重复数据，包含serverId
     */
    public static void checkUserRankListValidEx(LinkedList<CrossUserRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        Map<Long, Long> hasUserIdList = new HashMap<>();
        for (int i = 0; i < rankList.size(); i++) {
            CrossUserRank userRank = rankList.get(i);
            if (hasUserIdList.containsKey(userRank.getUserId()) && hasUserIdList.get(userRank.getUserId()) == userRank.getUserBaseInfo().getServerId()){
                needRemoveList.add(i);
            }else{
                hasUserIdList.put(userRank.getUserId(), userRank.getUserBaseInfo().getServerId());
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    /**
     * 校验数组，去掉重复数据
     */
    public static void checkUnionRankListValid(LinkedList<UnionRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        List<String> hasUnionUidList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            UnionRank unionRank = rankList.get(i);
            if(!hasUnionUidList.contains(unionRank.getUnionUid())){
                hasUnionUidList.add(unionRank.getUnionUid());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    public static void checkUnionValueRankListValid(LinkedList<UnionValueRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        List<String> hasUnionUidList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            UnionValueRank unionRank = rankList.get(i);
            if(!hasUnionUidList.contains(unionRank.getUnionUid())){
                hasUnionUidList.add(unionRank.getUnionUid());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    public static void checkUnionWeekRankListValid(LinkedList<CrossUnionWeekRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        List<String> hasUnionUidList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            CrossUnionWeekRank unionRank = rankList.get(i);
            if(!hasUnionUidList.contains(unionRank.getUnionUid())){
                hasUnionUidList.add(unionRank.getUnionUid());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    /**
     * 校验数组，去掉重复数据
     */
    public static void checkUnionActivityRankListValid(LinkedList<CrossUnionActivityRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        List<String> hasUnionUidList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            CrossUnionActivityRank unionRank = rankList.get(i);
            if(!hasUnionUidList.contains(unionRank.getUnionUid())){
                hasUnionUidList.add(unionRank.getUnionUid());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }

    /**
     * 校验数组，去掉重复数据
     */
    public static void checkTeamActivityRankListValid(LinkedList<CrossActivityTeamRank> rankList){
        List<Integer> needRemoveList = new ArrayList<>();
        List<Integer> hasTeamIdList = new ArrayList<>();
        for (int i = 0; i < rankList.size(); i++) {
            CrossActivityTeamRank teamRank = rankList.get(i);
            if(!hasTeamIdList.contains(teamRank.getTeamId())){
                hasTeamIdList.add(teamRank.getTeamId());
            }else {
                needRemoveList.add(i);
            }
        }
        if(needRemoveList.size() > 0){
            Collections.sort(needRemoveList);
            Collections.reverse(needRemoveList);
            for (int index : needRemoveList) {
                rankList.remove(index);
            }
        }
    }
}
